const mysql = require('mysql');
const fs=require('fs');
const http=require("http");
const path=require('path');

var con=mysql.createConnection(
    {
        host:'192.168.42.144',
        port:3306,
        user:'root',
        password:"ddd",
        database:'mydatabase'
    }
)
var selectSql='select chapterName,imgPath,chapterDes,publishTimer,author,views from chaptert';
var chapterlist=[];
con.query(selectSql,(err,result)=>{
    if(err){
        console.log(err);
    }else{
        const filepath=path.join(__dirname,'/images');
        var files=fs.readdirSync(filepath);
        // console.log(result[0].imgPath);
        for(var i=0;i<result.length;i++){
            chapterlist[i]={
                chapterName:result[i]['chapterName'],
                // imgPath:result[i]['imgPath'],
                imgPath:'/images/'+files[i],
                chapterDes:result[i]['chapterDes'],
                publishTimer:result[i]['publishTimer'],
                author:result[i]['author'],
                views:result[i]['views'],
                chapterId:i,
                chapterContent:result[i]['content']
            }
        }
        
    }
})

function getData(str){
    var arr=str.split("&");
    var obj={};
    for(var i=0;i<arr.length;i++){
        var childArr=arr[i].split('=');
        obj[childArr[0]]=childArr[1];
    }
    return obj;
}
http.createServer((req,res)=>{
    var urlObj=new URL(req.url,"http://localhost:8084/");
    var pathName=urlObj.pathname;
    if(pathName=='/'){           
        var filePath=path.join(__dirname,'/chapterList.html')
        var fileContent=fs.readFileSync(filePath);

        res.writeHead(200,{"Content-Type":"text/html;charset=utf8"});
        res.write(fileContent);
        res.end();
    }
    else if(pathName=='/getChapterList'){

        var chapterStr = JSON.stringify(chapterlist);
        res.writeHead(200, { "Content-Type": "text/plain" });
        res.write(chapterStr);
        res.end();
    }else if(req.url.indexOf('css/')>=0){
        var filePath=path.join(__dirname,'/'+req.url);
        var fileContent=fs.readFileSync(filePath);
        res.writeHead(200,{"Content-Type":'text/css'});
        res.write(fileContent);
        res.end();
    }else if(req.url.indexOf('images/')>=0){
        var imgPath=path.join(__dirname,'/'+req.url);
        var imgContent=fs.readFileSync(imgPath);
        res.writeHead(200,{"Content-Type":'image/jpg'});
        res.write(imgContent);
        res.end();
    }else if(req.url.indexOf('/baiduTemplate.js')>=0){
        var filePath=path.join(__dirname,'/js/baiduTemplate.js');
        var fileContent=fs.readFileSync(filePath);
        res.writeHead(200,{"Content-type":'text/js'});
        res.write(fileContent);
        res.end();
    }else if(req.url.indexOf('/getDetail')>=0){
        var sqlStr='select * from chaptert where id=?'

        var arr=req.url.split('=');//!拆分成数组，数组的第二个值即为id值
        var chapterId=arr[i];

        var chapterDetail=[];
        con.query(sqlStr,[chapterId],(err,result)=>{
            if(err){
                console.log(err);
            }else{
                console.log(result);

                var chapterDetail=JSON.stringify(chapterlist);
                res.setHeader('Access-Control=Allow-Orign','*');
                res.write(chapterDetail);
                res.end();
            }
        })
    }
}).listen(8084)
